const https = require('https')
const fs = require('fs')
const {stdout} = process

const SRC_PATH = 'https://publicsuffix.org/list/effective_tld_names.dat'
const DST_PATH = '../src/tld-data.js'


stdout.write('loading')

https.get(SRC_PATH, res => {
  let str = ''
  res.on('data', s => {
    str += s
    stdout.write('.');
  }).on('end', _ => {
    proc(str)
  }).setEncoding('utf8')
})


function proc(str) {
  const list = str
    .split('\n')
    .filter(v => v)
    .filter(v => !v.startsWith('//'))
    .filter(v => !v.startsWith('!'))
    .map(v => v.replace('*.', ''))
    .sort((a, b) => a > b ? 1 : -1)
    .join(',')

  const result = `\
// THIS FILE WAS GENERATED BY './tools/tld/gen.js'
export default '${list}'
`
  fs.writeFileSync(DST_PATH, result)

  console.log('\nok')
}
